package com.letoken.platform.repo.app.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.util.BeanUtil;
import com.letoken.platform.repo.app.po.Token;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 * @author： letokenlabs
 * @date： 2021/11/24 9:55
 */
public class AppJsonStringRedis {
    private StringRedisTemplate stringRedisTemplate;

    private AppJsonStringRedis() {
        stringRedisTemplate = BeanUtil.getBean(StringRedisTemplate.class);
    }

    private enum Singleton {
        INSTANCE;
        private AppJsonStringRedis instance;

        // JVM会保证此方法绝对只调用一次
        Singleton() {
            instance = new AppJsonStringRedis();
        }
    }


    public static AppJsonStringRedis me() {
        return AppJsonStringRedis.Singleton.INSTANCE.instance;
    }

    public void set(String key, Object obj) {
        if (obj != null) {
            stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(obj));
        }
    }


    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void setValue(String key, String obj, int expireSeconds) {
        if (obj != null) {
            if (expireSeconds > 0)
                stringRedisTemplate.opsForValue().set(key, obj, expireSeconds, TimeUnit.SECONDS);
            else
                stringRedisTemplate.opsForValue().set(key, obj);
        }
    }
}
